﻿using Newtonsoft.Json;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Expression.Entity;
using T.FAS.Runtime.MF.Dao;
using T.FAS.Runtime.MF.SPI;
using  T.ServiceFramework.ServiceGateway.Client;
using  T.ServiceFramework.ServiceGateway.ClientExtension;
using YamlDotNet.Core.Tokens;

namespace T.FAS.Runtime.MF.Activities
{
    public class FasExpService : IFasExpService
    {
        public ExprResult DQExecute(string jsString, Dictionary<string, object> inputParam)
        {
            // return ExprService.GetInstance.DQExecute(jsString, inputParam);
            throw new NotImplementedException();
        }

        public ExprResult Execute(string jsString, FASContext fASContext, Dictionary<string, object> inputParam)
        {
            // return ExprService.GetInstance.Execute(jsString, fASContext, inputParam);

            var dicParams = new Dictionary<string, string>();
            foreach (var key in inputParam.Keys)
            {
                dicParams.Add(key, Newtonsoft.Json.JsonConvert.SerializeObject(inputParam[key]));
            }
            return new SGHttpClient(true).PostCurrentIDC<ExprResult>("FASR-JSExecute", dicParams, 20).data;
        }

        public ExprResult JSExecute(string jsString, Dictionary<string, object> inputParam)
        {
            // return ExprService.GetInstance.JSExecute(jsString, inputParam);
            throw new NotImplementedException();
        }

        public ExprResult Debug(string jsString, Dictionary<string, object> inputParam, Dictionary<string, string> extraParams=null)
        {
            // return ExprService.GetInstance.JSExecute(jsString, inputParam);
            var result = new ExprResult();
         
            var dicParams = new Dictionary<string, string>();
            dicParams.Add("expr", jsString);
            dicParams.Add("inputParam", Newtonsoft.Json.JsonConvert.SerializeObject(inputParam));
            //foreach (var key in inputParam.Keys)
            //{
            //    dicParams.Add(key, Newtonsoft.Json.JsonConvert.SerializeObject(inputParam[key]));
            //}
           

            try
            {
                LogUtil.Log($"jsString={JsonConvert.SerializeObject(jsString)}");
                //var executResult = new SGHttpClient(true).PostCurrentIDC<Dictionary<string, object>>("FASR-ExprDebugInternal", dicParams, 30).data;
                var executResult = new SGHttpClient(true).PostCurrentServiceSet<Dictionary<string, object>>("FASR-ExprDebugInternal", dicParams, 30).data;
                LogUtil.Log($"executResult={JsonConvert.SerializeObject(executResult)}");
                object state, resultObj, errMsg;
                ResultState enumState;
                if (executResult.TryGetValue("State", out state) && Enum.TryParse<ResultState>(state.ToString(), out enumState))
                {
                    result.State = enumState;
                }
                if (executResult.TryGetValue("Result", out resultObj))
                {
                    result.Result = resultObj;
                }
                if (executResult.TryGetValue("ErrMsg", out errMsg))
                {
                    result.ErrMsg = errMsg.ToString();
                }
            }
            catch (Exception ex)
            {
                LogUtil.Log($"Debug={ex.ToString()}");
                result.State = ResultState.Fail;
                result.ErrMsg = ex.Message;
            }
           
            return result;
        }

        public ExprResult JSExecuteNotReport(string jsString, Dictionary<string, object> inputParam)
        {
            //  return ExprService.GetInstance.JSExecuteNotReport(jsString, inputParam);
            throw new NotImplementedException();
        }
    }
}
